﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="en">

<head>
    <jsp:include page="/common.jsp"></jsp:include>
</head>

<body class="navbar-top">
<div class="modal-shiftfix">
    <jsp:include page="/menu.jsp"></jsp:include>
    <div class="container-fluid main-content">
        <div class="widget-container fluid-height clearfix">
            <div class="heading clearfix">
                <i class="icon-reorder"></i> 新增销售单 <a href="javascript:;" class="pull-right"
                                                      onclick="history.go(-1);"><i class="icon-reply"></i></a>
            </div>
            <div class="widget-content padded clearfix">
                <form id="billForm" class="form-horizontal" action="${pageContext.request.contextPath}/OrderServlet?method=submitOrders" method="post">
                    <div class="form-body">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0" style="margin-bottom:10px;">
                            <tr>
                                <td align="right"
                                    style="font-weight:bold; color:red; font-size:18px; line-height:40px;">——
                                    ${orders.orderSeq}
                                </td>
                                <input type="hidden" name="orderSeq" value="${orders.orderSeq}">
                            </tr>
                            <tr>
                                <td class="form-inline">
                                    <label>客户：</label>
                                    <input type="hidden" name="customer.customerId" id="customer_id" value="">
                                    <input type="text" value=""
                                           class="form-control weiBoxs-dialog selectCustomer" id="customer_name"
                                           dialog-title="选择客户" dialog-size="large"
                                           dialog-url="/customer/dialog-list.html" onclick="showCusotmerModel()" autocomplete="off">
                                    <label style="margin-left:10px;">主题：</label>
                                    <input type="text" name="title" class="form-control" id="subject" value="">
                                    <label style="margin-left:10px;">销售日期：</label>
                                    <input type="text" id="w0" class="form-control form_datetime"
                                           value="<fmt:formatDate value="${orders.createTime}" pattern="yyyy-MM-dd"/>" readonly style="width:170px;">

                                    <div style="float:right;">
                                        <label style="margin-left:10px;">制单人：</label>
                                        <input type="hidden" name="operator.employeeId" value="${orders.operator.employeeId}">
                                        <input type="text" class="form-control" value="${orders.operator.employeeName}"
                                               readonly="true">
                                    </div>
                                </td>
                            </tr>
                        </table>
                        <div class="row-fluid">
                            <div class="span12">
                                <table class="table table-bordered" id="no-input-border" width="95%" border="0"
                                       cellspacing="1" cellpadding="0">
                                    <thead>
                                    <tr>
                                        <th colspan="12" valign="center">商品信息</th>
                                        <th style="border: none;float: right;">
                                            <input type="button" class="btn btn-primary weiBoxs-dialog"
                                                   dialog-callback="productCallBacks" dialog-title="选择产品"
                                                   dialog-size="large" dialog-url="/product/dialog-list.html"
                                                   value="+添加产品" onclick="addOrderDetail()">
                                        </th>
                                    </tr>
                                    <tr style="background-color:#E0E8FF;text-align:center;">
                                        <td style="width: 2%;">&nbsp;</td>
                                        <td style="width: 15%;">商品</td>
                                        <td>规格</td>
                                        <td>数量</td>
                                        <td>售价</td>
                                        <td>折扣率(%)</td>
                                        <td>折扣额</td>
                                        <td>税率(%)</td>
                                        <td>税额</td>
                                        <td>税前</td>
                                        <td>税后</td>
                                        <td style="width: 10%;">仓库</td>
                                        <td>备注</td>
                                    </tr>
                                    </thead>
                                    <tbody id="view_row">
                                    </tbody>
                                    <tbody id="add_products">
                                    </tbody>
                                    <tbody>
                                    <tr style="background-color:#FFFFF3">
                                        <td></td>
                                        <td>合计</td>
                                        <td></td>
                                        <td id="total_amount_val">0</td>
                                        <td></td>
                                        <td></td>
                                        <td id="total_product_discount_val">0</td>
                                        <td></td>
                                        <td id="total_tax_price_val">0</td>
                                        <td id="total_no_tax_price_val">0</td>
                                        <td id="total_prime_price_val">0</td>
                                        <td></td>
                                        <td></td>
                                    </tr>
                                    <tr style="background-color:#FFFFF1">
                                        <td>&nbsp;</td>
                                        <td>其他费用</td>
                                        <td colspan="2">
                                            <input type="text"  class="form-control"
                                                   placeholder="0.00" id="discount_price" value="0"
                                                   onkeyup="cal_discount_price()">
                                        </td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td>最终应付</td>
                                        <td colspan="2">
                                            <input type="text" name="totalMoney" class="form-control"
                                                   placeholder="0.00" id="total_price" readonly="true" value="">
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>地址</td>
                                        <td colspan="12">
                                            <input type="text" class="normal_input form-control" id="address" value=""
                                                   placeholder="发货地址...">
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>备注</td>
                                        <td colspan="12">
                                            <textarea style="min-height:50px;"
                                                      class="form-control"></textarea>
                                        </td>
                                    </tr>
                                    </tbody>
                                    <tfoot>
                                    <tr>
                                        <td style="text-align:center;" colspan="13">
                                            <input id="submitForm" class="btn btn-success" type="button" value="保存" onclick="submitOrderForm()">&nbsp;
                                            <input class="btn btn-default" type="button"
                                                   onclick="javascript:history.go(-1)" value="返回">&nbsp;
                                            <input type="hidden" name="orderDetailSize" id="orderDetailSize" value=""></td>
                                    </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>


<!-- 	顾客模态框开始 -->
<div class="modal fade" id="customerModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myCustomerModalLabel">选择顾客</h4>
            </div>
            <div class="modal-body">
                <table class="table" id="exampleCustomer">
                    <thead>
                    <tr>
                        <th>选择</th>
                        <th>姓名</th>
                        <th>所属行业</th>
                        <th>顾客来源</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${customerList}" var="customer">
                        <tr>
                            <td>
                                <input type="radio"  name ="radioName" style=" opacity: 1;margin-top: -6px " id="${customer.customerId}"/>
                            </td>
                            <td id="${customer.customerId}_lg">${customer.customerName}</td>
                            <td>${customer.field.fieldName}</td>
                            <td>${customer.source.sourceName}</td>
                            <input id="${customer.customerId}_address" type="hidden" value="${customer.address}"/>
                        </tr>
                    </c:forEach>
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" id="checkCustomerPass" class="btn btn-primary"
                        onclick="postCustomerCheckInfo()">确认</button>
                <button type="button" id="checkCustomerRefuse" class="btn btn-primary"
                        onclick="postCustomerCheckDocInfo()">取消</button>
            </div>
        </div>

    </div>
</div>
<!-- 顾客模态框结束-->

<!-- 	产品模态框开始 -->
<div class="modal fade" id="productModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myProductModalLabel">选择商品</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" id="rowCut">
                <table class="table" id="exampleProduct">
                    <thead>
                    <tr>
                        <th>选择</th>
                        <th>产品名</th>
                        <th>单位</th>
                        <th>单价</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${productList}" var="product">
                        <tr>
                            <td>
                                <input type="radio"  name ="radioName" style=" opacity: 1;margin-top: -6px " id="${product.productId}"/>
                            </td>
                            <td id="${product.productId}_prodName">${product.prodName}</td>
                            <td id="${product.productId}_prodStandard">${product.prodStandard}</td>
                            <td id="${product.productId}_costPrice">${product.costPrice}</td>
                            <input id="${customer.customerId}_remarks" type="hidden" value="${product.remarks}"/>
                        </tr>
                    </c:forEach>
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" id="checkProductPass" class="btn btn-primary"
                        onclick="postProductCheckInfo()">确认</button>
                <button type="button" id="checkProductRefuse" class="btn btn-primary"
                        onclick="postProductCheckDocInfo()">取消</button>
            </div>
        </div>

    </div>
</div>
<!-- 产品模态框结束 -->

<script>
    //点击提交表单
    function submitOrderForm() {
        //设置 该订单关联的 订单详情的个数
        $("#orderDetailSize").val($("#view_row tr").length) ;
        //表单提交
        $("#billForm").submit() ;
    }

    //点击选择顾客
    function showCusotmerModel() {
        $("#customerModal").modal("show");
    }

    //取消选择顾客
    function postCustomerCheckDocInfo() {
        $("#customerModal").modal("hide");
    }

    //确定选择顾客
    function postCustomerCheckInfo() {
        let id = $("#customerModal :checked").attr("id") ;
        let name = $("#"+id+"_lg").text();
        let address = $("#"+id+"_address").val() ;
        console.log("id:"+id+",name:"+name) ;
        //提交订单 给后台使用
        $("#customer_id").val(id) ;
        //显示
        $("#customer_name").val(name) ;
        $("#address").val(address)
        $("#customerModal").modal("hide");
    }
</script>

<script>
    //点击选择顾客
    function showProductModel(rowCut) {
        $("#rowCut").val(rowCut) ;
        $("#productModal").modal("show");
    }

    //取消选择顾客
    function postProductCheckDocInfo() {
        $("#productModal").modal("hide");
    }

    //确定选择顾客
    function postProductCheckInfo() {
        let id = $("#productModal :checked").attr("id") ;
        let prodName = $("#"+id+"_prodName").text();
        let prodStandard = $("#"+id+"_prodStandard").text() ;
        let costPrice = $("#"+id+"_costPrice").text() ;
        let remarks = $("#"+id+"_remarks").val() ;
        //提交订单 给后台使用
        let rowCut = $("#rowCut").val() ;
        $("#product_id_"+rowCut).val(id) ;
        $("#product_name_"+rowCut).val(prodName) ;
        $("#product_standard_"+rowCut).text(prodStandard) ;
        $("#product_remarks_"+rowCut).val(remarks) ;
        $("#product_amount_"+rowCut).val(1) ;
        $("#product_unit_price_"+rowCut).val(costPrice) ;
        $("#product_discount_rate_"+rowCut).val(0) ;
        $("#product_discount_"+rowCut).val(0) ;
        $("#product_tax_rate_"+rowCut).val(0) ;
        $("#product_tax_price_"+rowCut).val(0) ;
        $("#product_no_tax_price_"+rowCut).val(costPrice) ;
        $("#product_prime_price_"+rowCut).val(costPrice) ;
        $("#total_amount_val").text(parseFloat($("#total_amount_val").text())+costPrice);
        $("#total_price").val($("#total_price").val()+costPrice) ;
        $("#productModal").modal("hide");
    }
</script>

<script>
    function addOrderDetail() {
        let row = $("#view_row tr").length;
        let data = '<tr id="row_'+row+'">\n'+
'                                        <td style="text-align:center;">\n'+
'                                            <a href="javascript:void(0);" class="reduce_one"><i\n'+
'                                                    class="glyphicon glyphicon-minus"></i></a>\n'+
'                                        </td>\n'+
'                                        <td>\n'+
'                                            <input type="hidden" name="ordersDetailList['+row+'].product.productId" id="product_id_'+row+'"\n'+
'                                                   class="product_id" value="0">\n'+
'                                            <input type="text" id="product_name_'+row+'" class="form-control"\n'+
'                                                   onclick="showProductModel('+row+')" autocomplete="off">\n'+
'                                        </td>\n'+
'                                        <td><span id="product_standard_'+row+'"\n'+
'                                                  style="display:inline-block;min-width:50px;"></span></td>\n'+
'                                        <td>\n'+
'                                            <input type="text" name="ordersDetailList['+row+'].buyNums" id="product_amount_'+row+'"\n'+
'                                                   class="amount form-control" onkeyup="product_calculate('+row+')"\n'+
'                                                   onfocus="$(this).onlypressnum();">\n'+
'                                        </td>\n'+
'                                        <td>\n'+
'                                            <input type="text" name="ordersDetailList['+row+'].realPrices" id="product_unit_price_'+row+'"\n'+
'                                                   onkeyup="product_calculate('+row+')" class="form-control">\n'+
'                                        </td>\n'+
'                                        <td>\n'+
'                                            <input type="text" \n'+
'                                                   id="product_discount_rate_'+row+'" class="form-control"\n'+
'                                                   onkeyup="product_calculate('+row+')">\n'+
'                                        </td>\n'+
'                                        <td>\n'+
'                                            <input type="text" id="product_discount_'+row+'"\n'+
'                                                   class="product_discount form-control" readonly="readonly">\n'+
'                                        </td>\n'+
'                                        <td>\n'+
'                                            <input type="text"  id="product_tax_rate_'+row+'"\n'+
'                                                   onkeyup="product_calculate('+row+')" class="form-control">\n'+
'                                        </td>\n'+
'                                        <td>\n'+
'                                            <input type="text" id="product_tax_price_'+row+'" class="tax_price form-control"\n'+
'                                                   readonly="readonly">\n'+
'                                        </td>\n'+
'                                        <td>\n'+
'                                            <input type="text" id="product_no_tax_price_'+row+'"\n'+
'                                                   class="no_tax_price form-control" readonly="readonly">\n'+
'                                        </td>\n'+
'                                        <td>\n'+
'                                            <input type="text"  id="product_prime_price_'+row+'"\n'+
'                                                   class="prime_price form-control" readonly="readonly">\n'+
'                                        </td>\n'+
'                                        <td>\n'+
'                                            <div class="divSelect">\n'+
'                                                <select name="ordersDetailList['+row+'].store.storeId" id="product_warehouse_id_'+row+'"\n'+
'                                                        class="form-control" style="padding: 0px;">\n'+
'                                                    <c:forEach items="${storeList}" var="store">\n'+
'                                                        <option value="${store.storeId}">${store.storeName}</option>\n'+
'                                                    </c:forEach>\n'+
'                                                </select>\n'+
'                                            </div>\n'+
'                                        </td>\n'+
'                                        <td>\n'+
'                                            <input type="text"  id="product_remarks_'+row+'" class="form-control">\n'+
'                                        </td>\n'+
'                                    </tr>' ;
                $("#view_row").append(data) ;
    }
</script>
</body>

</html>
